package com.second.hand.trading.server.wb;

import com.alibaba.fastjson.JSONObject;

import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.stereotype.Component;
import lombok.extern.slf4j.Slf4j;

import javax.websocket.*;
import javax.websocket.server.PathParam;
import java.util.Date;
import java.util.Map;
import java.util.Optional;
import java.util.Set;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.ConcurrentHashMap;
@Component
@Slf4j
//@ServerEndpoint("/websocket/{username}")
public class WebSocket implements ApplicationRunner {

   private static AtomicInteger onlineClientNumber = new AtomicInteger(0);

    private static Map<String , Session> onlineClientMap = new ConcurrentHashMap<>();

    @Override
    public void run(ApplicationArguments args) throws Exception {
//        onlineClientMap.put("1", session);
//        onlineClientMap.put("2",session);
    }

    @OnOpen
    public void onOpen(Session session, @PathParam("username") String username) {
//        System.out.println("username"+username);
        onlineClientNumber.incrementAndGet();//在线数+1
//        onlineClientMap.put(session.getId(),session);
//        System.out.println("usename:"+username);
//        if ("1".equals(username) || "2".equals(username)) {
//            onlineClientMap.put(username, session);
//        }
//        onlineClientMap.put(username,session);
//        onlineClientMap.put("2",session);
        log.info("时间[{}]：与用户[{}]的连接成功，当前连接编号[{}]，当前连接总数[{}]",
                new Date().toLocaleString(),
                username,
                session.getId(),
                onlineClientNumber);
    }
    @OnClose
    public void onClose(Session session,@PathParam("username") String username){

    }
    @OnError
    public void onError(Session session, Throwable error,@PathParam("username") String username){

    }
    @OnMessage
    public void onMsg(String message, Session session){
        JSONObject jsonObject = JSONObject.parseObject(message);
//        System.out.println(jsonObject.getString("username"));
//        onlineClientMap.put(jsonObject.getString("username"),session);
//
//        System.out.println("session:"+onlineClientMap.get(jsonObject.getString("you")));
        if(jsonObject.getString("message").equals("初始化")){
            String username = jsonObject.getString("username");
            System.out.println("username:"+username);
            onlineClientMap.put(username, session);
        }else {
            String you = jsonObject.getString("you");
            System.out.println("message:"+message);
            onlineClientMap.get(you).getAsyncRemote().sendText(message);

        }
//            sendAllMessage(message,jsonObject.getString("you"));


    }

    private void sendAllMessage(String message,String you) {
//        Set<String> sessionIdSet = onlineClientMap.keySet(); //获得Map的Key的集合
////        System.out.println(sessionIdSet);
//        for (String sessionId : sessionIdSet) { //迭代Key集合
//            if(sessionId.equals("0")) {
//                Session session = onlineClientMap.get(sessionId); //根据Key得到value
//                session.getAsyncRemote().sendText(message);
//            }
//        }
        Session session = onlineClientMap.get(you);
        session.getAsyncRemote().sendText(message);
    }


}
